package com.lz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lz.service.HomeService;
import com.lz.utils.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by lz on 2018/5/21.
 */
@Controller
@RequestMapping("/home")
public class HomeController {

    @Autowired
    private HomeService homeService;

    /**
     * 通过期限查询收益率
     * @param request
     */
    @RequestMapping("/period")
    @ResponseBody
    public List<Map<String, Object>> queryPeriod(HttpServletRequest request){
        String period = request.getParameter("period");
        if (StringUtils.isNotBlank(period)){
            return homeService.queryPeriod(period);
        } else {
            return null;
        }
    }

    /**
     * 测试题推荐产品
     * @param request
     */
    @RequestMapping("/recommend")
    @ResponseBody
    public JSONObject recommend(HttpServletRequest request){
        String level = request.getParameter("level");
        String pageSize = request.getParameter("pageSize");//每页显式多少条记录
        String pageIndex = request.getParameter("pageIndex");//当前页号
        if (StringUtils.isBlank(level)){
            level = "2";
        }
        if (StringUtils.isBlank(pageSize)){
            pageSize = "20";
        }
        if (StringUtils.isBlank(pageIndex)){
            pageIndex = "1";
        }
        PageUtil pageUtil = homeService.recommend(level,pageSize,pageIndex);
        JSONObject object = (JSONObject) JSON.toJSON(pageUtil);
        return object;
    }
}
